/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nnet2;

import java.io.File;
import java.util.ArrayList;

import nnet2.gui.PlotPanel;
import nnet2.learning.TestMethods;
import nnet2.net.RMLP;

/**
 *
 * @author kalinskia
 */
class TestLearning {

    private RMLP nnet;
    @SuppressWarnings("unused")
	private File dataFile;
    private ArrayList<Double> data;

    public ArrayList<Double> getData() {
        return data;
    }

    public void setData(ArrayList<Double> data) {
        this.data = data;
    }

    public TestLearning(RMLP nnet,int size, double per, double dist, double randDist, double range) {
        this.nnet = nnet;
        this.data = TestMethods.sinWithDistortion(size, per, dist, randDist, range);
//        this.data = TestMethods.constant(100, 0.5);
    }

    public void train(int times) {
        for (int i = 0; i < times; i++) {
            System.out.format("iteration %d ", i);
            this.nnet.train(data, 0.000001f);
        }
    }

    public void visualTrain(int times) {
        this.nnet.train(data, 0.000001f);
        PlotPanel p = PlotPanel.showPlot(data, resArray());

        for (int i = 0; i < times; i++) {
            System.out.format("iteration %d ", i);
            p.setError(this.nnet.train(data, 0.000001f));
            p.setForcast(resArray());
            p.setIteration(i);
            //p.setError(i)
            p.repaint();
        }
    }

    public double test() {
        double err, tot = 0;
        for (int i = 0; i < data.size() - 1; i++) {
            err = data.get(i + 1) - nnet.calculateValues(new double[]{data.get(i)})[0];
            //System.out.format("e =\t%+.5f\n", err);
            tot += Math.abs(err);
        }
        return tot / data.size();
    }

    public void visualRep() {
        ArrayList<Double> result = resArray();
        PlotPanel.showPlot(data, result);
    }

    private ArrayList<Double> resArray() {
        ArrayList<Double> result = new ArrayList<Double>(data.size());
        for (int i = 0; i < data.size(); i++) {
            result.add(nnet.calculateValues(new double[]{data.get(i)})[0]);
        }
        return result;
    }
}
